package com.xjzd.std.network.zerocopy;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {

    public static void main(String[] args) throws IOException {
        System.out.println("main pid: " + Thread.currentThread().getId());
        try (ServerSocket serverSocket = new ServerSocket()) {
            serverSocket.bind(new InetSocketAddress(9999));
            serverSocket.setReuseAddress(true);
            for (; ; ) {
                Socket socket = serverSocket.accept();
                InputStream inputStream = socket.getInputStream();
                int n;
                while ((n = inputStream.readAllBytes().length) > 0) {
                    System.out.println("server receive: " + n + " bytes");
                }
                System.out.println("close connection from " + socket.getRemoteSocketAddress());
                inputStream.close();
                socket.close();
            }
        }
    }
}
